//
// Created by Semir on 2020/4/26.
//

#include <iostream>
#include <deque>
#include <string>
#include <map>
#include <stack>

using namespace std;

map<string, int> table = {{"add", 0}, {"poll", 1}, {"peek", 2}};
stack<int> S1;
stack<int> S2;
int n;
string op;

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> op;
        switch (table[op]) {
            case 0:
                int tmp;
                cin >> tmp;
                S1.push(tmp);
                break;
            case 1:
                if (!S2.empty()) {
                    S2.pop();
                } else if (!S1.empty()) {
                    while (!S1.empty()) {
                        int tmp = S1.top();
                        S1.pop();
                        S2.push(tmp);
                    }
                    S2.pop();
                }
                break;
            case 2:
                if (!S2.empty()) {
                    cout << S2.top() << endl;
                } else if (!S1.empty()) {
                    while (!S1.empty()) {
                        int tmp = S1.top();
                        S1.pop();
                        S2.push(tmp);
                    }
                    cout << S2.top() << endl;
                }
                break;
        }
    }
    return 0;
}